<template>
	<view class="container">
		<view class="set-items" v-if="user!=''">
			<view class="set-item">
				<view class="center">头像</view>
				<image :src="user.head_pic" @click="uploadFilePromise()" mode="widthFix" class="head-img"></image>
			</view>
			<view class="set-item">
				<view class="center">ID</view>
				<view class="right-text">{{user_id}}</view>
			</view>
			<view class="set-item" @click="navigateTo('/pages/center/nickname')">
				<view class="center">昵称</view>
				<view class="right-text">{{user.nickname}}</view>
			</view>
			<!-- <view class="set-item">
				<view class="center">手机号</view>
				<view class="right-text">{{user.mobile}}</view>
			</view> -->
		</view>
	</view>
</template>

<script>
	import md5 from '@/components/MD5.js'
	import {
		baseUrl
	} from '@/config/config.js'
	export default {
		data() {
			return {
				user: '',
			};
		},
		methods: {
			//获取用户信息
			async get_user_info() {
				const data = await this.$post('api/UserInfo')
				if (data.code == 200) {
					this.user = data.data.user
				} else {
					this.$u.toast(data.msg)
				}
			},
			uploadFilePromise() {
				let sign_prefix = getApp().globalData.sign_prefix
				let sign = md5.hax_md5('pathhead_pic' + 'user_id' + this.user_id + sign_prefix + this.user_token +
					sign_prefix).toLocaleLowerCase()
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						uni.uploadFile({
							url: baseUrl + '/api/UploadPic', // 仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							header: {
								token: this.user_token
							},
							formData: {
								user_id: this.user_id,
								path: 'head_pic',
								sign: sign
							},
							success: (result) => {
								let data = JSON.parse(result.data)
								if (data.code == 200) {
									this.user.head_pic = data.data.img
								} else {
									uni.$u.toast(data.msg)
								}
							}
						});
					},
					complete: (res) => {
						console.info({
							res
						})
					}
				});
			}
		},
		onLoad(e) {

		},
		onShow() {
			this.get_user_info()
		},
	}
</script>

<style lang="scss" scoped>
	@import "@/static/css/form.scss";

	.container {
		.logout-btn {
			margin-top: 40rpx;
			height: 88rpx;
			line-height: 88rpx;
			border-radius: 50rpx;
			color: $theme-color;
			font-size: 30rpx;
			background: #fff;
		}
	}
</style>